<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use app\user\model\UserModel;
use cmf\controller\HomeBaseController;
use think\Db;

class IndexController extends HomeBaseController
{

    /**
     * 前台用户首页(公开)
     */
    public function index()
    {
        $id        = $this->request->param("id", 0, "intval");
        $userModel = new UserModel();
        if($id==0&&session('user')){
            $user=session('user');
            $id=$user['id'];
        }
        $user      = $userModel->where('id', $id)->find();
        if (empty($user)) {
            $this->error("查无此人！");
        }
        $this->assign($user->toArray());
        $this->assign('user',$user);
        return $this->fetch(":index");
    }

    /**
     * 前台ajax 判断用户登录状态接口
     */
    function isLogin()
    {
        if (cmf_is_user_login()) {
            $this->success("用户已登录",null,['user'=>cmf_get_current_user()]);
        } else {
            $this->error("此用户未登录!");
        }
    }

    /**
     * 退出登录
    */
    public function logout()
    {
        session("user", null);//只有前台用户退出
        return redirect($this->request->root() . "/");
    }

     public function b_phone(){
         
        $userModel = new UserModel();
        if(request()->isPost()){
          $data=request()->param();
              $isOpenRegistration = cmf_is_open_registration();
               if (!$isOpenRegistration) {
                $errMsg = cmf_check_verification_code($data['username'], $data['certifycode']);
                if (!empty($errMsg)) {
                    $this->error($errMsg);
                }
            }
             $uid=cmf_get_current_user_id();
             $userModel = new UserModel();
         
             $user = $userModel->where('mobile', $data['username'])->find();
             if($user){
                $d_user=Db::name('user')->where('id',$uid)->find();
                $new_user['openid']=$d_user['openid'];
                $new_user['tj_user']=$d_user['tj_user'];
                Db::name('user')->where('id',$user['id'])->update($new_user);
                Db::name('user')->where('id',$uid)->delete();
                $user = $userModel->where('mobile', $data['username'])->find()->toArray();
                cmf_update_current_user($user);
                $token = cmf_generate_user_token($user['id'], 'weixin');
                if (!empty($token)) {
                    session('token', $token);
                }
             }else{
                $new_user['mobile']=$data['username'];
                Db::name('user')->where('id',$uid)->update($new_user);
             }
             $this->success('绑定成功！');
        }else{
            return  $this->fetch(':b_phone');
        }

    }

}
